package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
    private Connection conn;
    @Override
    public List<DeliveryAddress> squeryById(String userId) {
        try {
            conn = DBUtil.getConnection();
            List<DeliveryAddress> list = deliveryAddressDao.queryById(userId);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public DeliveryAddress squeryDABbId(Integer daId) {
        try {
            conn = DBUtil.getConnection();
            DeliveryAddress deliveryAddress = deliveryAddressDao.queryDABbId(daId);
            return deliveryAddress;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public int sinsert(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            int a = deliveryAddressDao.insert(contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();
            return a;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int supdate(DeliveryAddress deliveryAddress) {
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            int a = deliveryAddressDao.update(deliveryAddress);
            DBUtil.commitTransaction();
            return a;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int sdelete(Integer daId) {
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            int a = deliveryAddressDao.delete(daId);
            DBUtil.commitTransaction();
            return a;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
}
